using System;
using System.Drawing;
using System.ComponentModel;
using System.Collections.Generic;

namespace SCADAEditor.Component
{
    public class TextAnnotationComponent : ComponentBase
    {
        private string _content = "批注文本";
        private Font _font = new Font("宋体", 12);
        private Color _fontColor = Color.Black;
        private FlashInterval _flashInterval = FlashInterval.None;
        
        [Category("外观")]
        [Description("批注文本内容")]
        public string Content
        {
            get { return _content; }
            set { _content = value; OnPropertyChanged(); }
        }
        
        [Category("外观")]
        [Description("文本字体")]
        public Font Font
        {
            get { return _font; }
            set { _font = value; OnPropertyChanged(); }
        }
        
        [Category("外观")]
        [Description("文本颜色")]
        public Color FontColor
        {
            get { return _fontColor; }
            set { _fontColor = value; OnPropertyChanged(); }
        }
        
        [Category("行为")]
        [Description("闪烁间隔")]
        public FlashInterval FlashInterval
        {
            get { return _flashInterval; }
            set { _flashInterval = value; OnPropertyChanged(); }
        }

        public override void Draw(Graphics g)
        {
            using (var brush = new SolidBrush(_fontColor))
            {
                g.DrawString(_content, _font, brush, Position);
            }
        }

         public TextAnnotationComponent()
        {
            Size = new Size(53, 23);
            Text = ""; // 设置默认文本
            IsEditMode = true;
        }

        public override Dictionary<string, object> ToJson()
        {
            var dict = base.ToJson();
            dict.Add("Content", _content);
            dict.Add("Font", _font.Name);
            dict.Add("FontSize", _font.Size);
            dict.Add("FontColor", _fontColor.ToArgb());
            dict.Add("FlashInterval", (int)_flashInterval);
            return dict;
        }

        public override void FromJson(Dictionary<string, object> json)
        {
            base.FromJson(json);
            _content = (string)json["Content"];
            var fontName = (string)json["Font"];
            var fontSize = Convert.ToSingle(json["FontSize"]);
            _font = new Font(fontName, fontSize);
            _fontColor = Color.FromArgb((int)json["FontColor"]);
            _flashInterval = (FlashInterval)(int)json["FlashInterval"];
        }
    }

    public enum FlashInterval
    {
        None,
        HalfSecond,
        OneSecond
    }
}